package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionImplService implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    public void save (Permission permission) {
        permissionMapper.insert(permission);
    }

    public void updateById (Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    public void deleteById (Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    public Permission selectById (Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }


    public List<Permission> listAll () {
        return permissionMapper.selectAll();
    }

    public PageInfo<Permission> query (QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> result = permissionMapper.queryForList(qo);
        return new PageInfo<>(result);
    }

    @Autowired
    private ApplicationContext ctx;

    public void reload () {
        //获取所有的权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();

        //从spring容器中获取所有controller
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        //获取控制器对象的值
        Collection<Object> values = beans.values();
        System.out.println(values);
        //遍历获取每一个控制器
        for (Object controller : values) {
            //获取controller的字节码对象
            Class clazz = controller.getClass().getSuperclass();
            //获取控制器中所有方法
            Method[] methods = clazz.getDeclaredMethods();
            //遍历获取每一个方法
            for (Method method : methods) {
                //获取贴了自定义注解的方法
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                if (annotation != null) {
                    //获取注解中的权限名称name

                    //获取注解中的权限表达式
                    String expression = annotation.value()[0];
                    if (!permissions.contains(expression)) {
                        Permission permission = new Permission();
                        permission.setName( annotation.value()[1]);
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> selectByEmpId (Long id) {
        return permissionMapper.selectByEmpId(id);
    }
}
